package enterprises

import grails.test.*

class EnterpriseIntegrationTests extends GroovyTestCase {

	static final String TEST_ENTERPRISE = "Test Enterprise"

	static final Integer TEST_REGISTRATION_NUMBER = 555
	
	Enterprise enterprise
	
    protected void setUp() {				
        super.setUp()		
    }

    protected void tearDown() {
        super.tearDown()
    }   
	
	void testSaveEnterprise() {
		def enterprise = new Enterprise(name:TEST_ENTERPRISE, registrationNumber:TEST_REGISTRATION_NUMBER)		
		
		enterprise.addToProviders(new Provider(name:"Test_Provider", primaryProduct:"Test_Product"))
		enterprise.addToProviders(new Provider(name:"Test_Provider2", primaryProduct:"Test_Product2"))
		
		def sector = new IndustrialSector(name:"Test_Sector")
		def ownershipType = new TypeOwnership(name:"Test_Ownership")
		
		sector.save()
		ownershipType.save()
		
		enterprise.typeOwnership = ownershipType
		enterprise.industrialSector = sector
					
		enterprise.validate()
		
		print enterprise.errors
		
		assertNotNull enterprise.save()
	
		def foundEnterprise = Enterprise.get(enterprise.id)
		assertEquals 2, foundEnterprise.providers.size()
			
	}
	
	def testGetEnterprisesAndProductionsByTypeConsumer() {
		def enterprises = Enterprise.executeQuery("select e from Enterprise as e " 
											+ "join e.productions as p "
											+  "join p.consumer as c "
											+ "where c.typeConsumer.id = :typeConsumerId",
												[typeConsumerId:3L]);	
											
		println enterprises[0].productions;		
	}
	
	def testGetEnterpriseStatisticsByIndustrialSector() {
		def stat = IndustrialSector.executeQuery("select  e.industrialSector.name as sector, count(e.id), " +
												"sum(e.countWorkers), sum(e.itrCount), sum(e.manCount), sum(e.womenCount), " +
												 "sum(summaryArea), sum(freeArea), sum(possibleBuildingArea) from Enterprise as e "										
												+ "where e.industrialSector.id = :sectorId "
												+ "group by e.industrialSector.name"								
												, [sectorId:3L]);
		println "Enterprise statistics:" + stat;
	}
	
	def testGetEnterpriseCountByIndustrialSectorAndTypeOwnership() {
		def stat = IndustrialSector.executeQuery("select e.typeOwnership.name, count(e.id) " +
												"from Enterprise as e "
												+ "where e.industrialSector.id = :sectorId "
												+ "group by e.industrialSector, e.typeOwnership.name" 
												, [sectorId:3L]);
		println "Enterprise statistics by type ownership:" + stat;
	}
	
	def testGetProvidersStatisticsByIndustrialSector() {
		def stat = IndustrialSector.executeQuery("select  p.typeProvider.name, count(e.id) from Enterprise as e " +
												 "join e.providers as p "
												+ "where e.industrialSector.id = :sectorId "
												+ "group by e.industrialSector, p.typeProvider.name"
												, [sectorId:3L]
												);
		println "Providers statistics:" + stat;
	}
	
	def testGetConsumerStatisticsByIndustrialSector() {
		def stat = IndustrialSector.executeQuery("select c.typeConsumer.name, count(e.id) from Enterprise as e " +
			"join e.productions as p "
			+ "join p.consumer as c "
		   + "where e.industrialSector.id = :sectorId "
		   + "group by e.industrialSector, c.typeConsumer.name", [sectorId:3L]
		   );
	   
	   	println "Consumer statistics:" + stat;
	}
}
